// pages/search/search.js
import { proxy } from '../../utils/util.js';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '', //导航栏 中间的标题
      background: 'linear-gradient(to bottom, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0))',
      hasFilter: true,
      color: '#FFFFFF',
      btnStyle: 'white', //black, white
    },
    navBarHeight: app.globalData.height,
    hotKeyWord: ["风", "大海", "城市", "人像", "月", "世界", "纪实", "旅行", "黑白", "萌宠", "美", "微距"],
    searchList:true,
    searchType:"",
    urlStr: "",
    params: {}
  },
  handlerSearch:function(e){
    this.setData({
      searchList: true,
      keyWord:e.detail.value
    },()=>{
      let searchType = this.data.searchType;
      if (searchType) {
        this._tabclick({
          detail: {
            name: searchType
          }
        });
        return;
      }
      let tabEl = this.selectComponent('#searchTab')
      tabEl.setTabs([{
        displayName: '图片',
        name: 'photo'
      }, {
        displayName: '影集',
        name: 'set'
      }, {
        displayName: '活动',
        name: 'contest'
      }, {
        displayName: '摄影师',
        name: 'user'
      }], (item) => {
        this._tabclick({
          detail: {
            name: item.name
          }
        })
      })
    })
    
  },
  chooseHotKey:function(e){
    let value = e.currentTarget.dataset.keyword;
    let searchBox = this.selectComponent('#searchBox');
    searchBox.setData({
      InputValue:value
    });
    this.handlerSearch({
      detail:{
        value: value
      }
    })
  },
  _tabclick:function(e){
    let type = e.detail.name;
    let urlStr = `${proxy}/community/searchv2`;
    let params = {
      client_type:1,
      imgSize:'p2,p4',
      key:this.data.keyWord,
      searchType: type,
      page:1,
      size:20,
      type:'json'
    };
    if(type === 'photo'){
      params.avatarSize = 'a1';
      params.resourceType= '0,2';
    } else if (type === 'contest') {
      params.isFilterV3Contest = 'contest';
    } 
    this.setData({
      urlStr: urlStr,
      params: params,
      searchType: type
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let value = options.searchValue;
    let searchBox = this.selectComponent('#searchBox');
    searchBox.setData({
      InputValue: value
    });
    this.handlerSearch({
      detail: {
        value: value
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let params = this.data.params;
    params.page++;
    this.setData({
      params: params
    })
  },
  

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // }
})